﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsJobInvitationsTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// JobInvitations Business Object
    /// </summary>

    public class JobInvitations : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected JobInvitations() { }

        #endregion

        #region Public Properties

        #region Instance Properties


        /// <summary>
        /// JobID.
        /// </summary>
        public string JobID
        {
            get { return _jobID; }
            set
            {
                if (_jobID != value)
                {
                    if (OnPropertyChanging("JobID", _jobID, value))
                    {
                        string oldValue = _jobID;
                        _jobID = value;
                        OnPropertyChanged("JobID", oldValue, value);
                    }
                }
            }
        }  private string _jobID = null;


        /// <summary>
        /// InvitedBy.
        /// </summary>
        public int InvitedBy
        {
            get { return _invitedBy; }
            set
            {
                if (_invitedBy != value)
                {
                    if (OnPropertyChanging("InvitedBy", _invitedBy, value))
                    {
                        int oldValue = _invitedBy;
                        _invitedBy = value;
                        OnPropertyChanged("InvitedBy", oldValue, value);
                    }
                }
            }
        }  private int _invitedBy = -1;


        /// <summary>
        /// InvitedTo.
        /// </summary>
        public int InvitedTo
        {
            get { return _invitedTo; }
            set
            {
                if (_invitedTo != value)
                {
                    if (OnPropertyChanging("InvitedTo", _invitedTo, value))
                    {
                        int oldValue = _invitedTo;
                        _invitedTo = value;
                        OnPropertyChanged("InvitedTo", oldValue, value);
                    }
                }
            }
        }  private int _invitedTo = -1;


        /// <summary>
        /// CompanyName.
        /// </summary>
        public string CompanyName
        {
            get { return _companyName; }
            set
            {
                if (_companyName != value)
                {
                    if (OnPropertyChanging("CompanyName", _companyName, value))
                    {
                        string oldValue = _companyName;
                        _companyName = value;
                        OnPropertyChanged("CompanyName", oldValue, value);
                    }
                }
            }
        }  private string _companyName = null;


        /// <summary>
        /// Status.
        /// </summary>
        public string Status
        {
            get { return _status; }
            set
            {
                if (_status != value)
                {
                    if (OnPropertyChanging("Status", _status, value))
                    {
                        string oldValue = _status;
                        _status = value;
                        OnPropertyChanged("Status", oldValue, value);
                    }
                }
            }
        }  private string _status = null;


        /// <summary>
        /// Date/time at which the JobInvitations was created.
        /// </summary>
        public DateTime? InvitationDate
        {
            get { return _invitationDate; }
            set
            {
                if (_invitationDate != value)
                {
                    if (OnPropertyChanging("InvitationDate", _invitationDate, value))
                    {
                        DateTime? oldValue = _invitationDate;
                        _invitationDate = value;
                        OnPropertyChanged("InvitationDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _invitationDate = null;

        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty JobInvitations object.
        /// </summary>
        public static JobInvitations CreateNew()
        {
            JobInvitations obj = new JobInvitations();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            obj.InvitationDate = DateTime.Now;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static JobInvitations CreateNew(JobInvitationsCollection collection)
        {
            JobInvitations obj = CreateNew();
            collection.Add(obj);
            return (obj);
        }

        #endregion

        #region GetCompleteJobInvitations

        /// <summary>
        /// Returns a collection of JobInvitations.
        /// </summary>
        public static JobInvitationsCollection GetCompleteJobInvitations(string jobid)
        {
            JobInvitationsTableAdapter adapter = new JobInvitationsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsJobInvitations.JobInvitationsDataTable tbl = adapter.GetData(jobid);

            JobInvitationsCollection list = new JobInvitationsCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsJobInvitations.JobInvitationsRow row in tbl.Rows)
                {
                    JobInvitations JobInvitations = new JobInvitations();
                    RelationalToObject(JobInvitations, row);
                    list.Add(JobInvitations);
                }
            }

            return (list);
        }

        #endregion

        #region GetDataByJobIDnInvitedTo

        /// <summary>
        /// Returns a collection of JobInvitations.
        /// </summary>
        public static JobInvitationsCollection GetDataByJobIDnInvitedTo(string jobid, int invitedto)
        {
            JobInvitationsTableAdapter adapter = new JobInvitationsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsJobInvitations.JobInvitationsDataTable tbl = adapter.GetDataJobIDnInvitedTo(jobid, invitedto);

            JobInvitationsCollection list = new JobInvitationsCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsJobInvitations.JobInvitationsRow row in tbl.Rows)
                {
                    JobInvitations JobInvitations = new JobInvitations();
                    RelationalToObject(JobInvitations, row);
                    list.Add(JobInvitations);
                }
            }

            return (list);
        }

        #endregion

        #region GetDataByInvitedTo

        /// <summary>
        /// Returns a collection of JobInvitations.
        /// </summary>
        public static JobInvitationsCollection GetDataByInvitedTo(int invitedto)
        {
            JobInvitationsTableAdapter adapter = new JobInvitationsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsJobInvitations.JobInvitationsDataTable tbl = adapter.GetDataByInvitedTo(invitedto);

            JobInvitationsCollection list = new JobInvitationsCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsJobInvitations.JobInvitationsRow row in tbl.Rows)
                {
                    JobInvitations JobInvitations = new JobInvitations();
                    RelationalToObject(JobInvitations, row);
                    list.Add(JobInvitations);
                }
            }

            return (list);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified JobInvitations.
        /// </summary>
        public static void Update(JobInvitations obj)
        {
            bool updateJobInvitations = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateJobInvitations = (obj.ObjState != ObjectState.Unchanged);

                if (!updateJobInvitations)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsJobInvitations.JobInvitationsDataTable tbl = null;
            if (updateJobInvitations)
            {
                JobInvitationsTableAdapter adapter = new JobInvitationsTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();


                tbl = new dsJobInvitations.JobInvitationsDataTable();
                dsJobInvitations.JobInvitationsRow row = tbl.NewJobInvitationsRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("JobID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("JobInvitations: JobID={0}, CompanyName={1}, {2}", JobID, CompanyName));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more SubAdmin objects.
    /// </summary>

    public class JobInvitationsCollection : List<JobInvitations>
    {
        #region Types

        public class UserComparer : IComparer<JobInvitations>
        {
            public int Compare(JobInvitations ji1, JobInvitations ji2)
            {
                return (String.Compare(ji1.JobID, ji2.JobID));
            }
        }

        #endregion

        #region Public Methods

        #region FindByJobID

        public JobInvitations FindByJobID(string JobID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].JobID == JobID)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion

    }
}
